package cn.edu.zjicm.wordsnet_d.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.adapter.WordInnerPagerAdapter;
import cn.edu.zjicm.wordsnet_d.data.Affix;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.view.PageControl;

public class WordInnerFragment extends Fragment implements OnClickListener {

	private ViewPager mViewPager;
	private PageControl pointImg;
	private OneWord mOneWord;
	private boolean isTestMode;
	private boolean isTestMode2; // 是否是测试模式2
	private TextView mAffixExchangeText;
	private LinearLayout mAffixExchange;
	private TextView affixDivider1, affixDivider2;
	private int currentPageItem = 0;
	private int nextItem = 0;
	private boolean isMoving = false;
	private float XDown = -1;

	private int[] points;

	WordInnerPagerAdapter wordInnerPagerAdapter;
	private boolean hasLoaded[] = new boolean[3];

	public WordInnerFragment() {

	}

	public WordInnerFragment(OneWord mOneWord, boolean isTestMode, boolean isTestMode2) {
		this.mOneWord = mOneWord;
		this.isTestMode = isTestMode;
		this.isTestMode2 = isTestMode2;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		long start = System.currentTimeMillis();

		View view = inflater.inflate(R.layout.fragment_word_inner, null);
		mViewPager = (ViewPager) view.findViewById(R.id.word_inner_pager);
		pointImg = (PageControl) view.findViewById(R.id.word_inner_point);
		mAffixExchangeText = (TextView) view.findViewById(R.id.affix_exchange_text);
		mAffixExchange = (LinearLayout) view.findViewById(R.id.affix_exchange);
		affixDivider1 = (TextView) view.findViewById(R.id.affix_divider1);
		affixDivider2 = (TextView) view.findViewById(R.id.affix_divider2);

		showAffix();

		mViewPager.setOffscreenPageLimit(3);

		wordInnerPagerAdapter = new WordInnerPagerAdapter(getActivity(), mOneWord, isTestMode);
		mViewPager.setAdapter(wordInnerPagerAdapter);

		int currentItem = 0;
		if (isTestMode2) {
			if (mOneWord.getSense_degree_fm() / 10 == 2) {
				if (mOneWord.getLemma_type() == 2 && wordInnerPagerAdapter.isShowSameShape()) // 词组，显示形近词
					currentItem = 1;
				else if (mOneWord.getLemma_type() != 2 && wordInnerPagerAdapter.isShowAffix()) // 非词组，显示词根词缀
					currentItem = 1;
			} else if (mOneWord.getSense_degree_fm() / 10 == 3 && wordInnerPagerAdapter.isShowSameShape()) { // 显示形近词
				if (wordInnerPagerAdapter.isShowAffix()) {
					currentItem = 2;
				} else {
					currentItem = 1;
				}
			}
		}
		mViewPager.setCurrentItem(currentItem);
		wordInnerPagerAdapter.setInitPageIndex(currentItem);
		hasLoaded[currentItem] = true;

		// if (wordInnerPagerAdapter.getCount() == 3) {
		// points = new int[] { R.drawable.dot3_1, R.drawable.dot3_2,
		// R.drawable.dot3_3 };
		// pointImg.setImageResource(points[currentItem]);
		// } else if (wordInnerPagerAdapter.getCount() == 2) {
		// points = new int[] { R.drawable.dot2_1, R.drawable.dot2_2 };
		// pointImg.setImageResource(points[currentItem]);
		// } else
		// pointImg.setVisibility(View.GONE);
		pointImg.setPointCount(wordInnerPagerAdapter.getCount());
		pointImg.setCurrentIndex(currentItem);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				if (0 != position) {
					mAffixExchange.setVisibility(View.GONE);
				} else if (mOneWord.isShowAffixTab()) {
					mAffixExchange.setVisibility(View.VISIBLE);
				}
				loadData(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				if (isMoving) {
					pointImg.moving(currentPageItem, nextItem, arg1, true);
				}
			}

			@Override
			public void onPageScrollStateChanged(int position) {
				
				if (position == 0) {// 滑动结束
					currentPageItem = mViewPager.getCurrentItem();
				} else if (position == 2) {// 滑动手指离开屏幕
					isMoving = false;
					XDown = -1;
					pointImg.leftActionAnimation(currentPageItem, mViewPager.getCurrentItem());
					currentPageItem = mViewPager.getCurrentItem();
				}
			}
		});
		mViewPager.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_MOVE:
					if (XDown > 0 && event.getX() > XDown && currentPageItem > 0) {
						nextItem = currentPageItem - 1;
						isMoving = true;
					} else if (XDown > 0) {
						nextItem = currentPageItem + 1;
						isMoving = true;
					}
					if (!isMoving) {
						XDown = event.getX();
					}
					break;
				}
				return false;
			}
		});
		return view;
	}

	private void loadData(int position) {
		if (!hasLoaded[position]) {
			wordInnerPagerAdapter.initData(position);
			hasLoaded[position] = true;
		}
	}

	@Override
	public void onClick(View v) {
		if (v == mAffixExchange) {
			mViewPager.setCurrentItem(1);
			loadData(1);
		}
	}

	// 词根词缀功能
	private void showAffix() {
		if (mOneWord.isShowAffixTab()) {
			mAffixExchangeText.setText(mOneWord.getAff_content());
			mAffixExchange.setVisibility(View.VISIBLE);
			// 设置词根词缀
			mAffixExchange.setClickable(true);
			mAffixExchange.setOnClickListener(this);
			Affix affix = WordFactory.getInstance(getActivity()).getWordRoot(mOneWord.getIndex_word_id());
			if (affix != null) {
				affixDivider1.setVisibility(View.VISIBLE);
				affixDivider2.setVisibility(View.VISIBLE);
			} else {
				affixDivider1.setVisibility(View.GONE);
				affixDivider2.setVisibility(View.GONE);
			}
		}
	}
}
